package com.rrx.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rrx.model.Fault;
import com.rrx.service.FaultService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2018/8/13.
 */

@Controller
public class FaultController {
    @Resource
    FaultService faultService;
    ObjectMapper mapper = new ObjectMapper();

    /**
     * 故障类型信息查询
     * @param
     * @return
     */
    @RequestMapping("/wxSelectFault")
    @ResponseBody
    public String selectFaultType() throws JsonProcessingException {
        List<Fault> faults=faultService.selectFaultType();
        String json = mapper.writeValueAsString(faults);
        return json;
    }

    @RequestMapping("/wxSelectFaultinfo")
    @ResponseBody
    public String selectFaultinfo(String faulttype,HttpServletRequest request) throws JsonProcessingException {
        faulttype=request.getParameter("faulttype");
        List<Fault> faultinfo=faultService.selectFaultinfo(faulttype);
        String json = mapper.writeValueAsString(faultinfo);
        return json;


    }

    @RequestMapping("/addFault")
    @ResponseBody
    public String addFault(HttpServletRequest request) {
        String faultName = request.getParameter("fault_type");
        String faultInfo = request.getParameter("fault_info");
        if(faultName != null && faultInfo != null && faultName.trim() != "" && faultInfo.trim() != "") {
            if(faultService.addFault(faultName, faultInfo))
                return "success";
        }
        return "error";
    }

    @RequestMapping("/getFaultType")
    @ResponseBody
    public String getFaultType() {
        String[] faults = faultService.getFaultType();
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(faults);
            return json;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }
    @RequestMapping("/getFaultByType")
    @ResponseBody
    public String getFaultByType(HttpServletRequest request) {
        String type = request.getParameter("type");
        if(!(type != null && type.trim() != "")) {
            return "";
        }
        Fault[] faults = faultService.getFaultByType(type);
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(faults);
            return json;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

    @RequestMapping("/updateFault")
    @ResponseBody
    public String updateFault(HttpServletRequest request) {
        String requestId = request.getParameter("fault_id");
        String requestType = request.getParameter("fault_type_input");
        String requestInfo = request.getParameter("fault_info_input");
        String requestState = request.getParameter("fault_state_checkbox");
        System.out.println(requestType+"---"+requestInfo);
        int id = -1;
        int state = -1;
        try {
            id = Integer.parseInt(requestId);
            state = Integer.parseInt(requestState);
        }catch(Exception e) {}
        if(id == -1)
            return "idError";
        if(state == -1)
            return "stateError";
//        if(!(requestType != null && requestType.trim() != ""))
//            return "typeError";
//        if(!(requestInfo != null && requestInfo.trim() != ""))
//            return "infoError";
        if(faultService.updateFault(id, requestType, requestInfo, state))
            return "success";
        return "error";
    }
}
